package com.wtwd.device.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtwd.device.model.vo.ResultVO;
import com.wtwd.device.constant.RespCode;
import com.wtwd.device.model.entity.AppManagement;
import com.wtwd.device.model.entity.AppVersion;
import com.wtwd.device.model.vo.AppManagementVo;
import com.wtwd.device.model.vo.AppManagmentListVo;
import com.wtwd.device.model.vo.AppNameVo;
import com.wtwd.device.model.vo.RespEntity;
import com.wtwd.device.service.AppManagementService;
import com.wtwd.device.service.AppVersionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author mjy
 * @date 2021/11/08
 * @description
 */
@Api(tags = "App管理模块")
@RestController
@RequestMapping("/app-management")
public class AppManagementController {

    private static final Logger logger = LoggerFactory.getLogger(AppManagementController.class);

    @Autowired
    private AppManagementService appManagementService;

    @Autowired
    private AppVersionService appVersionService;


    /**
     * 分页获取App管理列表
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "分页获取App管理列表", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "int")
            , @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataType = "int")
    })
    @GetMapping("/getAppManagementList")
    public ResultVO<AppManagmentListVo> getAppManagementList(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage
            , @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        logger.info("getWatchList param  " + currentPage, pageSize);
        Page<Object> page = new Page<>(currentPage, pageSize);
        IPage<AppManagement> appManagements = appManagementService.getPagingAppManagementList(page);
        AppManagmentListVo appManagmentListVo = new AppManagmentListVo();
        appManagmentListVo.setTotal(appManagements.getTotal());
        appManagmentListVo.setAppManagements(appManagements.getRecords());
        return ResultVO.success(RespCode.SUCCESS, appManagmentListVo);
    }


    /**
     * 添加app
     *
     * @param appManagementVo
     * @return
     */
    @ApiOperation(value = "添加app", notes = "")
    @PostMapping("/addApp")
    public RespEntity addApp(@RequestBody AppManagementVo appManagementVo) {
        logger.info("addApp param  " + appManagementVo.toString());
        AppManagement appManagement = new AppManagement();
        appManagement.setAppName(appManagementVo.getAppName());
        RespEntity respEntity = checkAddApp(appManagement);
        if (respEntity.getCode() != RespCode.SUCCESS.getCode()) {
            return respEntity;
        }
        appManagement.setCreateTime(new Date().getTime());
        appManagement.setUpdateTime(0L);
        Integer i = appManagementService.addApp(appManagement);//添加时返回主键
        Integer rest = 0;
        if (i > 0) {
            rest = initAppVersion(appManagementVo, appManagement.getId());
        }
        return new RespEntity(rest > 0 ? RespCode.SUCCESS : RespCode.FAILED);
    }

    /**
     * 修改app
     *
     * @param appManagement
     * @return
     */
    @ApiOperation(value = "修改app(暂时去除修改功能)", notes = "修改app时，主键id必传")
    @PutMapping("/updateApp")
    public RespEntity updateApp(@RequestBody AppManagement appManagement) {
        logger.info("updateApp param  " + appManagement.toString());
        RespEntity respEntity = checkUpdateApp(appManagement);
        if (respEntity.getCode() != RespCode.SUCCESS.getCode()) {
            return respEntity;
        }
        appManagement.setUpdateTime(new Date().getTime());
        return appManagementService.updateApp(appManagement);
    }

    /**
     * 删除app
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除app", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "Long")
    })
    @DeleteMapping("/deleteApp/{id}")
    public RespEntity deleteApp(@PathVariable("id") long id) {
        logger.info("deleteApp param  " + id);
        return appManagementService.deleteAppById(id);
    }


    /**
     * 获取所有app名称以及主键id
     *
     * @return
     */
    @ApiOperation(value = "获取所有app名称", notes = "")
    @GetMapping("/getAllAppName")
    public ResultVO<List<AppNameVo>> getAllAppName() {
        logger.info("getAllAppName");
        return appManagementService.getAllAppName();
    }

    public Integer initAppVersion(AppManagementVo appManagementVo, Long appId) {
        ArrayList<AppVersion> appVersions = new ArrayList<>();
        AppVersion androidVersion = new AppVersion();
        androidVersion.setAppId(appId);
        androidVersion.setUpdateTime(0L);
        androidVersion.setCreateTime(new Date().getTime());
        androidVersion.setType(0);
        androidVersion.setDescribe("app安卓初始版本");
        androidVersion.setVersion(appManagementVo.getAndroidVersion());
        androidVersion.setClientType(2);
        appVersions.add(androidVersion);
        AppVersion iosVersion = new AppVersion();
        iosVersion.setAppId(appId);
        iosVersion.setUpdateTime(0L);
        iosVersion.setCreateTime(new Date().getTime());
        iosVersion.setType(0);
        iosVersion.setDescribe("appIos初始版本");
        iosVersion.setVersion(appManagementVo.getIosVersion());
        iosVersion.setClientType(1);
        appVersions.add(iosVersion);
        return appVersionService.bathAddAppVersion(appVersions);
    }

    /**
     * 校验修改app时规则
     *
     * @param appManagement
     * @return
     */
    public RespEntity checkUpdateApp(AppManagement appManagement) {
        if (appManagement.getId() == null || StringUtils.isBlank(appManagement.getAppName())) {
            return new RespEntity(RespCode.NO_PARAM);
        }
        AppManagement appManagement1 = appManagementService.getAppManagementByName(appManagement.getAppName());
        if (appManagement1 != null) {
            if (appManagement1.getId() != appManagement.getId()) {//不是同一组数据，修改之后同名
                return new RespEntity(RespCode.APP_NAME_REPEAT);
            }
        }
        return new RespEntity(RespCode.SUCCESS);
    }


    /**
     * 校验app添加时规则
     *
     * @param appManagement
     * @return
     */
    public RespEntity checkAddApp(AppManagement appManagement) {
        if (StringUtils.isBlank(appManagement.getAppName())) {//参数不能为空
            return new RespEntity(RespCode.NO_PARAM);
        }
        AppManagement appManagement1 = appManagementService.getAppManagementByName(appManagement.getAppName());
        if (appManagement1 != null) {
            return new RespEntity(RespCode.APP_NAME_REPEAT);
        }
        return new RespEntity(RespCode.SUCCESS);
    }



}
